<?php

/**
 * Name: 消息模型层
 * User: Silent
 * Date: 2025-10-09
 * Time: 15:20:49
 */

namespace App\Models\Message;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Message extends Model
{
    use HasFactory, SoftDeletes;

    protected static array $ignoreChangedAttributes = [
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected static array $logAttributes = [

    ];

    protected static bool $logOnlyDirty = true;

    protected $table = 'messages';

    protected $guarded = [];

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
    ];

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }



    /**
     * 格式化类型.
     *
     * @return string
     */
    public function getFormatTypeAttribute(): string
    {
        switch ($this->type) {
            case 'risk_sources':
                return '风险源';
            case 'accidents':
                return '生产事故';
            case 'hazards':
                return '安全隐患';
            case 'questions':
                return '问题清单';
            default:
                return '-';
        }
    }

    public function userMessages()
    {
        return $this->hasMany(UserMessage::class, 'message_id');
    }
}
